import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Order, OrderDetail, Part, PartWarrantyCategory, ProductCategory, VehicleInfo, Detail, Brand} from './DTO';
import {baseDataStatus, warrantyPolicyStatus, vehicleStatus} from '../Enum';
import {PAGE} from '../constants';
const DEFAULT_ORDER_QUERY = {
    status: [warrantyPolicyStatus.新建],
    pageIndex: PAGE.index,
    pageSize: 100,
    sortField: 'startDate',
    isDesc: true
};
const baseUrl = '/afterSales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '备件索赔政策'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

const enableDesc = formatMessage({
    id: 'api.message.enableDes',
    defaultMessage: '生效{resourceName}'
}, {resourceName});

const getOrderDetailDesc = formatMessage({
    id: 'api.message.getOrderDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const partDesc = formatMessage({
    id: 'api.message.partDesc',
    defaultMessage: '查询索赔备件'
});

const partWarrantyCategoriesDesc = formatMessage({
    id: 'api.message.partWarrantyCategoriesDesc',
    defaultMessage: '查询备件索赔分类'
});

const productCategoryDesc = formatMessage({
    id: 'api.message.productCategoryDesc',
    defaultMessage: '查询产品分类'
});

const vehiclesDesc = formatMessage({
    id: 'api.message.getVehiclesDesc',
    defaultMessage: '查询车辆信息'
});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询备件索赔条款详情'
});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

// 主页查询 查询条款
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partWarrantyPolicyItems?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};


// 查询条款详情
export const getDetail = id => request(`${baseUrl}/partWarrantyPolicyItems/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));


// 查询备件索赔政策
export const getOrders = data => {
    const queryString = stringify({
        ...DEFAULT_ORDER_QUERY,
        status: data.status
    });
    return request(`${baseUrl}/partWarrantyPolicies?${queryString}`, null, Order)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 作废
export const abandonOrders = data => request(`${baseUrl}/partWarrantyPolicies/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));

// 生效
export const enableOrders = data => request(`${baseUrl}/partWarrantyPolicies/enable`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, enableDesc));


// 查询备件索赔政策详情
export const getOrderDetail = id => request(`${baseUrl}/partWarrantyPolicies/${id}`, null, OrderDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getOrderDetailDesc));


// 新增
export const addOrder = data => request(`${baseUrl}/partWarrantyPolicies`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updateOrder = (id, data) => request(`${baseUrl}/partWarrantyPolicies/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 查询索赔备件信息
export const getParts = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/parts/forClaimApply?${queryString}`, null, Part)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partDesc));
};

// 查询产品分类
export const getProductCategories = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/productCategories?${queryString}`, null, ProductCategory)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, productCategoryDesc));
};

// 查询车辆信息
export const getVehicles = queryCondition => {
    const queryString = stringify({
        ...queryCondition,
        status: [vehicleStatus.实销完成, vehicleStatus.经销商仓库]
    });
    return request(`${baseUrl}/vehicleSolds?${queryString}`, null, VehicleInfo)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, vehiclesDesc));
};


// 查询备件索赔分类
export const getPartWarrantyCategories = condition => {
    const queryString = stringify({
        ...condition,
        status: baseDataStatus.生效,
    });
    return request(`${baseUrl}/partWarrantyCategories?${queryString}`, null, PartWarrantyCategory)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, partWarrantyCategoriesDesc));
};

// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};
//导入车辆模板
export const importTemp = () => request(`${baseUrl}/partWarrantyPolicies/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
